home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / lists / mint / l_0799 / 685 / asmtrans.zoo / trans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-05  |  5.2 KB  |  274 lines

  1. /* asmtrans translator
  2.    Copyright 1992,1993 Eric R. Smith
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /*
  20.  * translation routines; sometimes we pass data on
  21.  * through directly (asm syntax->asm syntax, with
  22.  * some macro expansion), and other times we also
  23.  * do asm syntax->gas syntax translation
  24.  */
  25.  
  26. #include "asmtrans.h"
  27.  
  28. int syntax = GAS;
  29.  
  30. char *
  31. immediate(op)
  32.     char *op;
  33. {
  34.     return concat("#", op);
  35. }
  36.  
  37. char *
  38. indirect(op)
  39.     char *op;
  40. {
  41.     if (syntax == GAS) {
  42.         return concat(op, "@");
  43.     } else {
  44.         return concat3("(", op, ")");
  45.     }
  46. }
  47.  
  48. char *
  49. postinc(op)
  50.     char *op;
  51. {
  52.     if (syntax == GAS) {
  53.         return concat(op, "@+");
  54.     } else {
  55.         return concat3("(", op, ")+");
  56.     }
  57. }
  58.  
  59. char *
  60. predec(op)
  61.     char *op;
  62. {
  63.     if (syntax == GAS) {
  64.         return concat(op, "@-");
  65.     } else {
  66.         return concat3("-(", op, ")");
  67.     }
  68. }
  69.  
  70. char *
  71. indexed(op1, op2)
  72.     char *op1, *op2;
  73. {
  74.     if (syntax == GAS) {
  75.         return concat4(op2, "@(", op1, ")");
  76.     } else {
  77.         return concat4(op1, "(", op2, ")");
  78.     }
  79. }
  80.  
  81. char *
  82. sizedop(op, size)
  83.     char *op, *size;
  84. {
  85.     if (syntax == GAS) {
  86.         return changesiz(concat(op, size));
  87.     } else {
  88.         return concat4("(", op, ")", size);
  89.     }
  90. }
  91.  
  92. char *
  93. twoindex(disp, base, index)
  94.     char *disp, *base, *index;
  95. {
  96.     if (syntax == GAS) {
  97.         return concat6(base, "@(", disp, ",", changesiz2(index), ")");
  98.     } else {
  99.         return concat6(disp, "(", base, ",", index, ")");
  100.     }
  101. }
  102.  
  103. char *
  104. bitfield(op1, op2, op3)
  105.     char *op1, *op2, *op3;
  106. {
  107.     if (syntax == GAS) {
  108.         return concat6(op1, "{#", op2, ":#", op3, "}");
  109.     } else {
  110.         return concat6(op1, "{", op2, ":", op3, "}");
  111.     }
  112. }
  113.  
  114. char *
  115. postindex(bd, an , index, od)
  116.     char *bd, *an, *index , *od;
  117. {
  118.     if (syntax == GAS) {
  119.         return concat8(an, "@(", bd, ")@(", od, ",", changesiz2(index), ")");
  120.     } else {
  121.         return concat9("([",an, ",", bd, "],", index, ",", od ,")");
  122.     }
  123. }
  124.  
  125. char *
  126. postindex0(bd)
  127.     char *bd;
  128. {
  129.     if (syntax == GAS) {
  130.         return concat3("@(", bd, ")");
  131.     } else {
  132.         return concat3("([", bd, "])");
  133.     }
  134. }
  135.  
  136. char *
  137. postindex1(bd,od)
  138.     char *bd, *od;
  139. {
  140.     if (syntax == GAS) {
  141.         return concat5("@(", bd, ")@(", od, ")");
  142.     } else {
  143.         return concat5("([", bd, "],", od, ")");
  144.     }
  145. }
  146.  
  147. char *
  148. preindex(bd, an , index, od)
  149.     char *bd, *an, *index , *od;
  150. {
  151.     if (syntax == GAS) {
  152.         return concat8(an, "@(", bd, ",", changesiz2(index), ")@(", od, ")");
  153.     } else {
  154.         return concat9("([",an, ",", bd, ",", index, "],", od, ")");
  155.     }
  156. }
  157.  
  158. char *
  159. do_ops(label, opcode, space, operand)
  160.     char *label, *opcode, *space, *operand;
  161. {
  162.     static char temp[LINSIZ];
  163.     static char optemp[40];
  164.     char *to, *from;
  165.     char c;
  166.  
  167.     if (syntax == GAS) {
  168.         if (!strcmp(opcode, "ds.l")) {
  169.         strcpy(temp, "\t.even\n\t.comm");
  170.         strcat(temp, space);
  171.         strcat(temp, label);
  172.         strcat(temp, ",");
  173.         strcat(temp, "4*");
  174.         strcat(temp, operand);
  175.         return strdup(temp);
  176.         } else if (!strcmp(opcode, "ds.w")) {
  177.         strcpy(temp, "\t.even\n\t.comm");
  178.         strcat(temp, space);
  179.         strcat(temp, label);
  180.         strcat(temp, ",");
  181.         strcat(temp, "2*");
  182.         strcat(temp, operand);
  183.         return strdup(temp);
  184.         } else if (!strcmp(opcode, "ds.b")) {
  185.         strcpy(temp, "\t.comm");
  186.         strcat(temp, space);
  187.         strcat(temp, label);
  188.         strcat(temp, ",");
  189.         strcat(temp, operand);
  190.         return strdup(temp);
  191.         } else {
  192.         to = optemp;
  193.         from = opcode;
  194.         for(;;) {
  195.             c = *from++;
  196.             if (!c) break;
  197.     /* change 'foo.b' -> 'foob' */
  198.     /* special case: bra.s -> 'bra', since gas automatically
  199.      * selects offset sizes and since some gas's actually
  200.      * mess up if an explicit '.s' is given
  201.      */
  202.             if (c == '.' && *from && from[1] == 0) {
  203.                 if (*from == 's') from++;
  204.                 continue;
  205.             }
  206.             *to++ = c;
  207.         }
  208.         *to = 0;
  209.         opcode = optemp;
  210.         }
  211.     }
  212.  
  213.     to = temp;
  214.  
  215.     if (*label) {
  216.         int colonseen = 0;
  217.         char c;
  218.  
  219.         for (from = label; (c = *from++) != 0;) {
  220.             if (c == ':') colonseen = 1;
  221.             *to++ = c;
  222.         }
  223. /* gas labels must have a ':' after them */
  224.         if (!colonseen && syntax == GAS)
  225.             *to++ = ':';
  226.     }
  227.  
  228.     *to++ = '\t';
  229.     strcpy(to, opcode);
  230.     strcat(temp, space);
  231.     strcat(temp, operand);
  232.     return strdup(temp);
  233. }
  234.  
  235. char *
  236. changesiz(op)
  237.     char *op;
  238. {
  239.     char *r = op;
  240.  
  241.     if (syntax != GAS) return op;
  242.  
  243.     while (*op) {
  244.         if (*op == '.') *op = ':';
  245.         op++;
  246.     }
  247.     return r;
  248. }
  249.  
  250. char *
  251. changesiz2(op)        /* rw: hack for scaled index    */
  252.     char *op;
  253. {
  254.     char *r = op;
  255.  
  256.     if (syntax != GAS) return op;
  257.  
  258.     while (*op) {
  259.         if (*op == '.' || *op == '*' ) *op = ':';
  260.         op++;
  261.     }
  262.     return r;
  263. }
  264.  
  265. char *
  266. hexop(op)
  267.     char *op;
  268. {
  269.     if (syntax == GAS)
  270.         return concat("0x", op);
  271.     else
  272.         return concat("$", op);
  273. }
  274.